<template lang="html">
    <section class="real-app">
        <input class="add-input" type="text" name="" value="" autofocus="autofocus" placeholder="接下去要做什么？" @keyup.enter="addTodo">
        <Item :todo="todo" v-for="todo in filteredTodos" :key="todo.id" @del="deleteTodo" />
        <Tabs :filter="filter" :todos="todos" @toggle="toggleFilter" @clearAllCompleted="clearAllCompleted" />
        <!-- <router-view /> -->
    </section>
</template>

<script>
import Item from './item.vue'
import Tabs from './tabs.vue'
let id = 0

export default {
    // 组件内的守卫
    beforeRouteEnter (to, from, next) {
        console.log('todo before enter', this)
        // next()
        next(vm => {
            console.log('after enter this.id is', to.params.id)
            // console.log('after enter vm.id is', vm.id)
            // console.log(vm)
        })
    },
    // 举例来说，对于一个带有动态参数的路径 /foo/:id，在 /foo/1 和 /foo/2 之间跳转的时候
    // 建议根据后面的id获取数据的话 使用，否者就要使用watch 会加大开销
    beforeRouteUpdate (to, from, next) {
        console.log('todo update enter')
        next()
    },
    // 这个离开守卫通常用来禁止用户在还未保存修改前突然离开。该导航可以通过 next(false) 来取消。
    beforeRouteLeave (to, from, next) {
        console.log('todo leave enter')
        // next()
        // 老师案例
        // 加不加 global 没有区别
        if ( global.confirm('are you sure?') ) {
            next()
        }

        // 官方案例
        // const answer = window.confirm('Do you really want to leave? you have unsaved changes!')
        // if (answer) {
        //     next()
        // } else {
        //     next(false)
        // }
    },
    // props: ['id'],
    // props: {
    //     id: '456'
    // },
    // props: (route) => ({
    //     id: route.query.b
    // }),
    mounted () {
        // console.log(this.id)
        // 相同的路由下 只有一次会触发
        console.log('todo mounted')
    },
    data() {
        return {
            todos: [],
            filter: 'all'
        }
    },
    components: {
        Item,Tabs,
    },
    computed: {
        filteredTodos(){
            if(this.filter === 'all'){
                return this.todos
            }
            const completed = this.filter === 'completed'
            return this.todos.filter(todo => completed === todo.completed)
        }
    },
    methods: {
        addTodo(e){
            // unshift() 方法可向数组的开头添加一个或更多元素，并返回新的长度。
            this.todos.unshift({
                id: id++,
                // trim() 方法用于删除字符串的头尾空格,不会改变原始字符串。
                content: e.target.value.trim(),
                completed: false
            })
            e.target.value = ''
        },
        deleteTodo(id){
            // splice() 方法用于添加或删除数组中的元素。
            // findIndex() 方法返回传入一个测试条件（函数）符合条件的数组第一个元素位置。
            this.todos.splice( this.todos.findIndex(todo => todo.id === id), 1 )
        },
        toggleFilter(state){
            this.filter = state
        },
        clearAllCompleted(){
            this.todos = this.todos.filter(todo => !todo.completed)
        }
    }
}
</script>

<style lang="stylus" scoped>
.real-app
    width 600px
    margin 0 auto
    box-shadow 0 0 5px #666
.add-input
    position relative
    margin 0
    width 100%
    font-size 24px
    font-family inherit
    font-weight inherit
    line-height 1.4em
    border none
    outline none
    color inherit
    box-sizing border-box
    font-smoothing antialiased
    padding 16px 16px 16px 36px
    border none
    box-shadow inset 0 -2px 1px rgba(0, 0, 0, 0.03)
</style>
